﻿Imports System.Threading
Imports System.IO

Public Class frm_Timeidan
    Public Structure DataStructure
        Dim 本号 As String
        Dim 票号 As String
        Dim 条码数据 As String
    End Structure
    Dim Data As New List(Of DataStructure)
    Dim th As Thread
    Const BHCount As Integer = 25

    Private Sub frm_Timeidan_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        ChangeButtonFromFrm(Me.GroupBox1)
        ChangeButtonFromFrm(Me.GroupBox2)
        ChangeButtonFromFrm(Me.GroupBox3)
        AddHandler TB_BHLength.KeyPress, AddressOf HandleTextKeyPress
        AddHandler TB_PHLength.KeyPress, AddressOf HandleTextKeyPress
        AddHandler TB_PHStartNo.KeyPress, AddressOf HandleTextKeyPress
        AddHandler TB_PHEndNo.KeyPress, AddressOf HandleTextKeyPress
        AddHandler TB_BHStartNo.KeyPress, AddressOf HandleTextKeyPress
    End Sub
    Private Sub HandleTextKeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
        If Asc(e.KeyChar) = 8 Then Return
        If Asc(e.KeyChar) < 47 Or Asc(e.KeyChar) > 57 Then e.Handled = True
    End Sub
    Private Sub TB_DataFileName_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TB_DataFileName.KeyPress
        e.Handled = True
    End Sub

    Private Sub Btn_OpenDialog_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_OpenDialog.Click
        Dim dialog As New OpenFileDialog
        With dialog
            .Title = "选择客供号码数据"
            .CheckFileExists = True
            .Filter = "文本文件(*.txt)|*.txt"
            If Not String.IsNullOrEmpty(TB_SavePath.Text) Then
                .InitialDirectory = TB_SavePath.Text
            End If
            If .ShowDialog = Windows.Forms.DialogResult.OK Then
                Me.TB_DataFileName.Text = .FileName
                Me.TB_DataFileName.SelectionStart = .FileName.Length
                If String.IsNullOrEmpty(TB_SavePath.Text) Then
                    TB_SavePath.Text = getOnlyPath(TB_DataFileName.Text)
                End If
                TB_Name.Text = getOnlyName(.FileName)
            End If
        End With
    End Sub

    Private Sub Btn_OpenFolder_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_OpenFolder.Click
        Dim dialog As New FolderBrowserDialog
        With dialog
            .Description = "选择生成数据保存路径"
            .RootFolder = Environment.SpecialFolder.Desktop
            .ShowNewFolderButton = True
            If .ShowDialog = Windows.Forms.DialogResult.OK Then
                TB_SavePath.Text = .SelectedPath
                TB_SavePath.SelectionStart = .SelectedPath.Length
            End If
        End With
    End Sub

    Private Sub Btn_ReadData_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_ReadData.Click
        If File.Exists(TB_DataFileName.Text) Then
            SetObjectEnabled(False, GroupBox2)
            SetObjectEnabled(False, GroupBox3)
            Dim fs As New FileStream(TB_DataFileName.Text, FileMode.Open, FileAccess.Read)
            Dim fw As New StreamReader(fs, System.Text.Encoding.Default)
            Dim ErrText As String = String.Empty
            Do Until fw.EndOfStream
                Dim tmp As String = fw.ReadLine
                If Not String.IsNullOrEmpty(tmp) Then
                    Dim a As Integer = tmp.IndexOf(",")
                    If a > 0 Then
                        Dim str() As String = tmp.Split(",")
                        If str.Length <> 3 Then MessageBox.Show(Me, "数据格式错误，请确认数据格式是否正确！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error) : fw.Close() : fs.Close() : Data.Clear() : GoTo EndLine
                        Dim tmpItem As New DataStructure
                        tmpItem.票号 = str(0)
                        tmpItem.条码数据 = str(1)
                        tmpItem.本号 = str(2)
                        If Data Is Nothing Then Data = New List(Of DataStructure)
                        Data.Add(tmpItem)
                        My.Application.DoEvents()
                    ElseIf Not (String.IsNullOrEmpty(tmp)) Then
                        MessageBox.Show(Me, "数据格式错误，请确认数据格式是否正确！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error) : fw.Close() : fs.Close() : Data.Clear() : GoTo EndLine
                    End If
                End If
            Loop
            fw.Close()
            fs.Close()
            MessageBox.Show(Me, "数据读取结束，共有：" & Data.Count & "条数据！", "结束", MessageBoxButtons.OK, MessageBoxIcon.Information)
            ShowSampleData(RadioButton1.Checked)
EndLine:
            SetObjectEnabled(True, GroupBox2)
            SetObjectEnabled(True, GroupBox3)
        Else
            MessageBox.Show(Me, "文件不存在，请重新选择数据文件！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If
    End Sub
    Private Sub SetObjectEnabled(ByVal Enabled As Boolean, ByVal ControlPanel As Control)
        For Each item As Control In ControlPanel.Controls
            item.Enabled = Enabled
        Next
    End Sub
    Private Sub ShowSampleData(ByVal TiaoMa As Boolean)
        If Data.Count <= 0 Then Return
        If TiaoMa Then
            Dim SPS As Integer = Data.Count / 2
            ListBox1.Items.Clear()
            ListBox1.Items.Add("Var1   " & Data.Item(0).本号)
            ListBox1.Items.Add("Var2   " & Data.Item(0).票号)
            ListBox1.Items.Add("Var3   " & Data.Item(0).条码数据)
            ListBox1.Items.Add("Var4   " & Data.Item(SPS).本号)
            ListBox1.Items.Add("Var5   " & Data.Item(SPS).票号)
            ListBox1.Items.Add("Var6   " & Data.Item(SPS).条码数据)
        Else
            Dim SPS As Integer = Data.Count / 2
            ListBox1.Items.Clear()
            ListBox1.Items.Add("Var1   " & Data.Item(0).本号)
            ListBox1.Items.Add("Var2   " & Data.Item(0).票号)
            ListBox1.Items.Add("Var3   ")
            ListBox1.Items.Add("Var4   " & Data.Item(SPS).本号)
            ListBox1.Items.Add("Var5   " & Data.Item(SPS).票号)
            ListBox1.Items.Add("Var6   ")
        End If
    End Sub

    Private Sub Btn_CreateFM_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_CreateFM.Click
        Dim StartP As Long, EndP As Long
        StartP = Val(TB_PHStartNo.Text)
        EndP = Val(TB_PHEndNo.Text)
        If EndP <= StartP Then MessageBox.Show(Me, "起始票号不能大于或等于终止票号！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information) : Return
        If Directory.Exists(TB_SavePath.Text) = False Then MessageBox.Show(Me, "保存路径不存在，请先选择数据保存路径！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation) : Return
        If String.IsNullOrEmpty(TB_Name.Text) Then MessageBox.Show(Me, "保存文件名不能为空！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation) : Return
        If ((EndP - StartP) + 1) Mod 2 = 0 Then
            Dim PL As Long = Val(TB_PHLength.Text)
            Dim BL As Long = Val(TB_BHLength.Text)
            Dim PPrefix As String = TB_PHPrefix.Text
            Dim BPrefix As String = TB_BHPrefix.Text
            Dim OneEnd As Long = (EndP - StartP + 1) / 2 + StartP - 1
            Dim HalfCount As Long = (EndP - StartP + 1) / 2
            Dim HalfBHCount As Long = ((EndP - StartP + 1) \ 25) / 2
            Dim BHIndex As Long = 0
            Dim StartBH As Long = Val(TB_BHStartNo.Text)
            Dim FileName As String = TB_SavePath.Text
            NeroBar1.Maximum = ((EndP - StartP + 1) / 2) \ BHCount
            If Strings.Right(FileName, 1) <> "\" Then FileName = FileName & "\"
            Dim fs As New FileStream(FileName & TB_Name.Text & "封面.txt", FileMode.Create, FileAccess.Write)
            Dim fw As New StreamWriter(fs, System.Text.Encoding.Default)
            For i = StartP To OneEnd Step BHCount
                fw.WriteLine(BPrefix & FormatByLength(StartBH + BHIndex, BL))
                fw.WriteLine((PPrefix + FormatByLength(i, PL)) & "-" & (PPrefix + FormatByLength(i + BHCount - 1, PL)))
                '================
                fw.WriteLine(BPrefix & FormatByLength(StartBH + BHIndex + HalfBHCount, BL))
                fw.WriteLine(PPrefix + FormatByLength(HalfCount + i, PL) & "-" & PPrefix + FormatByLength(HalfCount + i + BHCount - 1, PL))
                NeroBar1.Value = i - StartP + 1
                BHIndex += 1
            Next
            fw.Close()
            fs.Close()
            MsgBox("数据生产结束，文件保存在：" & FileName & TB_Name.Text & "封面.txt", MsgBoxStyle.OkOnly + MsgBoxStyle.Information, "提示")
        Else
            MsgBox("双拼数据号码总数必须是2的倍数！", MsgBoxStyle.OkOnly + MsgBoxStyle.Information, "提示")
        End If
    End Sub

    Private Sub TB_SavePath_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TB_SavePath.KeyPress
        e.Handled = True
    End Sub

    Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton1.CheckedChanged
        ShowSampleData(RadioButton1.Checked)
    End Sub

    Private Sub RadioButton2_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles RadioButton2.CheckedChanged
        ShowSampleData(RadioButton1.Checked)
    End Sub

    Private Sub GdButton4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GdButton4.Click
        If Data.Count <= 0 Then Return
        Dim ForU As Integer = Data.Count / 2
        Dim FileName As String
        If Directory.Exists(TB_SavePath.Text) = False Then MessageBox.Show(Me, "保存路径不存在，请先选择数据保存路径！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation) : Return
        If String.IsNullOrEmpty(TB_Name.Text) Then MessageBox.Show(Me, "保存文件名不能为空！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation) : Return
        Dim tmp As String = TB_SavePath.Text
        If Strings.Right(tmp, 1) <> "\" Then tmp = tmp & "\"
        If RadioButton1.Checked Then
            FileName = tmp & TB_Name.Text & "-2.txt"
        Else
            FileName = tmp & TB_Name.Text & "-1.txt"
        End If
        SetObjectEnabled(False, GroupBox2)
        RadioButton1.Enabled = False
        RadioButton2.Enabled = False
        If File.Exists(FileName) Then
            If MessageBox.Show(Me, "文件已经存在是否覆盖原文件？", "询问", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Cancel Then
                FileName = getAddFileName(FileName)
            End If
        End If
        Dim isTiaoma As Boolean = RadioButton1.Checked
        Dim fs As New FileStream(FileName, FileMode.Create, FileAccess.Write)
        Dim fw As New StreamWriter(fs, System.Text.Encoding.Default)
        NeroBar2.Maximum = ForU
        For i = 0 To ForU - 1
            fw.WriteLine(Data.Item(i).本号)
            fw.WriteLine(Data.Item(i).票号)
            If isTiaoma Then
                fw.WriteLine(Data.Item(i).条码数据)
            Else
                If (i + 1) Mod BHCount = 0 Then
                    fw.WriteLine("-----------------------------------------------")
                Else
                    fw.WriteLine("")
                End If
            End If
            '====================================
            fw.WriteLine(Data.Item(i + ForU).本号)
            fw.WriteLine(Data.Item(i + ForU).票号)
            If isTiaoma Then
                fw.WriteLine(Data.Item(i + ForU).条码数据)
            Else
                If (i + 1) Mod BHCount = 0 Then
                    fw.WriteLine("------------------------------------------------")
                Else
                    fw.WriteLine("")
                End If
            End If
            NeroBar2.Value = i + 1
            My.Application.DoEvents()
        Next
        fw.Close()
        fs.Close()
        SetObjectEnabled(True, GroupBox2)
        RadioButton1.Enabled = True
        RadioButton2.Enabled = True
        MessageBox.Show(Me, "数据生成结束！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information)
    End Sub

    Private Sub GroupBox2_Enter(sender As Object, e As EventArgs) Handles GroupBox2.Enter

    End Sub
End Class